Emulating Motion
When developing an application that takes
advantage of the accelerometer, you can debug directly on a real device
if that makes the most sense. Otherwise, you can use the emulator to
emulate motion for your development. The emulator has a button on the
control bar to show a separate window with support for motion and
location emulation. To show this additional window, you have to click
the right-arrow icon on the control bar of the emulator, as shown in Figure 2.
FIGURE 2 Showing the Accelerometer window in the emulator
Clicking that button opens a window with an
Accelerometer tab and a Location tab. To emulate motion, you will use
the Accelerometer tab, as shown in Figure 3.
FIGURE 3 The Accelerometer window
You should notice the image
of a phone floating in space on the Accelerometer tab. In the middle of
the phone is a pink circle you can grab with your mouse to move it in
3D space. As you do that, the accelerometer values change (shown as X,
Y, and Z in the lower left). This will enable you to see the relative
axis values as you move the phone.
In the bottom-left area of the window is an
Orientation drop-down where you can change your perspective to the
phone. For example, if you change the orientation to Portrait Flat,
you’ll still see the phone, but it will be from the perspective of the
phone lying flat on a table.
Finally, in the bottom-right area of the window
is the Recorded Data drop-down that contains prerecorded sets of
accelerometer data that you can run. By default, the only recorded data
included is Shake, which emulates someone shaking the phone for a few
seconds.
Creating Recorded Data
You can add your recorded data to the emulator
by creating a simple XML file and dropping it into the emulator’s
accelerometer sensor data directory. The directory is as follows:
%PROGFILES%\Microsoft XDE\8.0\sensordata\acc
The XML files are labeled with the name you want
to appear in the drop-down list (for instance, “Shimmy” with no
extension for the Shake data file). The format of this XML file is shown
here:
<?xml version="1.0" encoding="utf-8"?>
<WindowsPhoneEmulator
xmlns="http://schemas.microsoft.com/WindowsPhoneEmulator/2009/08/
SensorData">
<SensorData>
<Header version="1"/>
<AccData offset="1000" x="1" y="0" z="0" />
<AccData offset="2000" x="-1" y="0" z="0" />
<AccData offset="3000" x="0" y="-1" z="0" />
</SensorData>
</WindowsPhoneEmulator>
The important part of the format is the SensorData
section. After the Header
element is a list of AccData
elements that contain the axis values as the number of milliseconds
after the start of the recording to change the value to. This example
moves the axis values after per second, but for something more complex
you might need to change the values much more often than that. The Shake
offsets change the values every few milliseconds to mimic the Shake
action.